import store from "@/store";
import storeState from "@/store/state.js";
let exincludes = [];

//登录关键数据
export function getLoginData() {
  try {
    let { userInfo } = JSON.parse(localStorage.getItem("LOGIN_DATA"));
    let access_token = localStorage.getItem("ACCESS_TOKEN");
    let { tenantCode } = store.state.ORGANIZATION || {};
    let { menuCode } = store.state.SYSTEM || {};
    return {
      userId: userInfo.uid,
      token: access_token,
      sysSource: menuCode,
      tenantCode: tenantCode,
      permissionCode: menuCode,
    };
  } catch (error) {
    return {};
  }
}

//数据存入缓存
export function saveData(key, value) {
  let isObj = isObject(value) || isArrary(value);
  store.dispatch("setStoreState", {
    [key]: value,
  });
  if (!exincludes.includes(key)) {
    localStorage.setItem(key, isObj ? JSON.stringify(value) : value);
  }
}

//清除系统缓存
export function clearStorageData() {
  localStorage.clear();
  Object.keys(storeState).forEach((key) => {
    let emu = {
      PAGE_REFRESH: 0,
      ACCESS_TOKEN: "",
      //登录信息
      LOGIN_DATA: {},
      //用户信息
      USER_INFO: {},
      //菜单权限
      PERMISIONS_DATA: [],
      //租户列表
      TENANT_DATA: [],
      //当前租户信息
      TENANT: {},
      //当前组织结构
      ORGANIZATION: {},
      //项目列表
      PROJECT_DATA: [],
      //当前系统
      SYSTEM: {},
      //数据看板
      DATA_BOARD: {},
      //业务数据
      YW_BOARD: [],
      //当前菜单
      CURRENT_MENU: {},
      //项目列表
      PROJECT_SOURCE: [],
      //待办列表
      TODO_DATA: [],
      //待办总数
      TODO_TOTAL: 0,
      //待办选中tab
      TODO_ACTIVEKEY: null,
      TODO_WEBSOCKET: 0,
      //业务数据
      SERVICE_SOURCE: [],
      //数据看板
      DATA_BOARD: {},
      //配置中心
      CONFIG_CENTER: [],
    };
    store.dispatch("setStoreState", {
      [key]: emu[key],
    });
  });
}

//将缓存数据同步到vuex
export function setDataToVuex() {
  Object.keys(storeState).forEach((key) => {
    let storeState = localStorage.getItem(key);
    if (storeState) {
      let data = "";
      try {
        data = JSON.parse(storeState);
      } catch (error) {
        data = storeState;
      }
      if (!exincludes.includes(key)) {
        store.dispatch("setStoreState", {
          [key]: data,
        });
      }
    } else {
    }
  });
}

//是否是对象类型
export function isObject(obj) {
  return Object.prototype.toString.call(obj) === "[object Object]";
}

//是否是空对象
export function isEmptyObject(obj) {
  return Object.keys(obj).length == 0;
}

//是否是数组
export function isArrary(arr) {
  return Array.isArray(arr);
}

//
export function getMenuData(type = 0) {
  let menuData = {};
  //业务数据
  let serviceData = store.state.SERVICE_SOURCE;
  //数据看板
  let dataBoard = store.state.DATA_BOARD;
  //欢迎页
  let welcomePage = {
    menuCode: "WELCOM_PAGE_HOME_PAGE",
    menuName: "欢迎页",
    path: "/welcome",
  };

  if (type == 0) {
    //进入欢迎页
    if (isEmptyObject(dataBoard) && serviceData.length == 0) {
      menuData = welcomePage;
    } else {
      //进入数据看板
      if (!isEmptyObject(dataBoard)) {
        menuData = dataBoard;
      } else {
        //进入数据看板
        menuData = getFirstMenu(serviceData);
      }
    }
  } else {
  }
  return { ...menuData, $$DATETIMESTR: new Date().getTime() };
}

//查询第一个有效菜单
export function getFirstMenu(menuData = []) {
  try {
    getData(menuData);
    function getData(data) {
      data.forEach((v) => {
        let { childs } = v;
        if (Array.isArray(childs) && childs.length > 0) {
          getData(childs);
        } else {
          throw v;
        }
      });
    }
  } catch (data) {
    return data;
  }
}
